package com.example.mqtt.service.impl;

import com.alibaba.fastjson2.JSON;
import com.example.mqtt.common.enums.QueueStatus;
import com.example.mqtt.config.WebSocketEndpoint;
import com.example.mqtt.mapper.QueueSequenceMapper;
import com.example.mqtt.model.Card;
import com.example.mqtt.model.WebSocketMessage;
import com.example.mqtt.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/5/3 16:01
 **/
@Service
@RequiredArgsConstructor
public class WebSocketServiceImpl implements WebSocketService {
    private final WebSocketEndpoint webSocketEndpoint;
    private final QueueSequenceMapper queueSequenceMapper;
    @Override
    public void sendAddCard(Card card) {
        String message = JSON.toJSONString(
                new WebSocketMessage("add_card", card)
        );
        webSocketEndpoint.broadcast(message);
    }

    @Override
    public void sendRemoveTopCard() {
        String message = JSON.toJSONString(
                new WebSocketMessage("remove_top_card", null)
        );
        webSocketEndpoint.broadcast(message);
    }

    @Override
    public void consumeQueue(String sequenceId, QueueStatus queueStatus) {
        queueSequenceMapper.updateQueueSeqStatus(sequenceId, queueStatus.getCode());
    }
}
